﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLNSTL
{
    public partial class Thong_Tin_Nhan_Vien : Form
    {
        private int maNV = -1;

        private bool General_Loaded = false;
        private bool ThongTinTrinhDo_Loaded = false;
        private bool ThongTinTuyenDung_Loaded = false;

        private NhanVien nv;

        public Thong_Tin_Nhan_Vien(int MaNV)
        {
            this.maNV = MaNV;
            QLNS_TLDataContext dc = new QLNS_TLDataContext();
            var tmp = from n in dc.NhanViens
                      where n.maNV == MaNV
                      select n;
            nv = tmp.FirstOrDefault();

            InitializeComponent();
        }

        public Thong_Tin_Nhan_Vien()
        {
            nv = new NhanVien();

            InitializeComponent();
        }

        private void General_Enter(object sender, EventArgs e)
        {
            if (!General_Loaded)
            {
                QLNS_TLDataContext dc = new QLNS_TLDataContext();

                #region Đổ dữ liệu vào control
                //ComboBox cbDanToc
                var dantocs = from dt in dc.DAN_TOCs
                              select dt;
                cbDanToc.DataSource = dantocs;
                cbDanToc.DisplayMember = "tenDanDoc";
                cbDanToc.ValueMember = "maDanToc";

                //ComboBox cbTonGiao
                var tongiaos = from dt in dc.TON_GIAOs
                              select dt;
                cbTonGiao.DataSource = tongiaos;
                cbTonGiao.DisplayMember = "tenTonGiao";
                cbTonGiao.ValueMember = "maTonGiao";

                //ComboBox cbQuocTich
                var quoctichs = from dt in dc.QUOC_TICHes
                              select dt;
                cbQuocTich.DataSource = quoctichs;
                cbQuocTich.DisplayMember = "tenQuocTich";
                cbQuocTich.ValueMember = "maQuocTich";

                //ComboBox cbTinhTrangHonNhan
                var tinhtranghonnhans = from dt in dc.TINHTRANGHONNHANs
                              select dt;
                cbTinhTrangHonNhan.DataSource = tinhtranghonnhans;
                cbTinhTrangHonNhan.DisplayMember = "tenTinhTrangHonNhan";
                cbTinhTrangHonNhan.ValueMember = "maTinhTrangHonNhan";
                #endregion

                #region Gán dự liệu cho control
                
                txtHoTen.Text = nv.hoTen;
                txtTenKhac.Text = nv.tenGoiKhac;
                txtSdt.Text = nv.soDT;
                txtCMND.Text = nv.cmnd;
                txtNoiCap.Text = nv.noiCap;
                if (nv.ThongTinCaNhan != null)
                {
                    txtQueQuan.Text = nv.ThongTinCaNhan.queQuan;
                    txtNangKhieu.Text = nv.ThongTinCaNhan.nangKhieu;
                    txtDCTT.Text = nv.ThongTinCaNhan.diaChiThuongTru;
                    if (nv.ThongTinCaNhan.maDanToc != null)
                    {
                        cbDanToc.SelectedValue = nv.ThongTinCaNhan.maDanToc; 
                    }
                    if (nv.ThongTinCaNhan.maTonGiao != null)
                    {
                        cbTonGiao.SelectedValue = nv.ThongTinCaNhan.maTonGiao; 
                    }
                    if (nv.ThongTinCaNhan.maQuocTich != null)
                    {
                        cbQuocTich.SelectedValue = nv.ThongTinCaNhan.maQuocTich; 
                    }
                }

                txtDCHT.Text = nv.diaChiHienTai;
                if (nv.gioiTinh != null)
                {
                    if ((bool)nv.gioiTinh)
                    {
                        rdNam.Checked = true;
                    }
                    else
                        rdNu.Checked = true;
                }
                else
                    rdNam.Checked = true;
                

                #endregion 
                General_Loaded = true;
            }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void tabPage2_Enter(object sender, EventArgs e)
        {
            if (!ThongTinTrinhDo_Loaded)
            {
                QLNS_TLDataContext dc = new QLNS_TLDataContext();
                #region Đổ dữ liệu vào control
                //ComboBox cbTrinhDoVanHoa
                var tdvh = from dt in dc.TRINH_DO_HOC_VANs
                              select dt;
                cbTrinhDoVanHoa.DataSource = tdvh;
                cbTrinhDoVanHoa.DisplayMember = "tenTrinhDoHocVan";
                cbTrinhDoVanHoa.ValueMember = "maHV";

                //ComboBox cbNganh
                var nganhs = from dt in dc.NGANHs
                              select dt;
                cbNganh.DataSource = nganhs;
                cbNganh.DisplayMember = "tenNganh";
                cbNganh.ValueMember = "maNganh";

                //ComboBox cbHinhThucDaoTao
                var hinhthucdaotaos = from dt in dc.HINHTHUCDAOTAOs
                              select dt;
                cbHinhThucDaoTao.DataSource = hinhthucdaotaos;
                cbHinhThucDaoTao.DisplayMember = "moTa";
                cbHinhThucDaoTao.ValueMember = "maHinhThuc";

                //ComboBox cbNoiDaoTao
                var noidaotaos = from dt in dc.NOIDAOTAOs
                              select dt;
                cbNoiDaoTao.DataSource = noidaotaos;
                cbNoiDaoTao.DisplayMember = "tenNoiDaoTao";
                cbNoiDaoTao.ValueMember = "maNoiDaoTao";

                //ComboBox cbNgoaiNgu
                var ngoaingus = from dt in dc.NGOAINGUs
                              select dt;
                cbNgoaiNgu.DataSource = ngoaingus;
                cbNgoaiNgu.DisplayMember = "tenNN";
                cbNgoaiNgu.ValueMember = "maNN";

                //ComboBox cbTrinhDoTinHoc
                var trinhdotinhocs = from dt in dc.TRINH_DO_TIN_HOCs
                              select dt;
                cbTrinhDoTinHoc.DataSource = trinhdotinhocs;
                cbTrinhDoTinHoc.DisplayMember = "tenTrinhDoTinHoc";
                cbTrinhDoTinHoc.ValueMember = "MaTDTH";
                #endregion

                #region Gán dự liệu cho control

                if (nv.TRINHDO != null)
                {
                    if (nv.TRINHDO.MaHV != null)
                    {
                        cbTrinhDoVanHoa.SelectedValue = nv.TRINHDO.MaHV; 
                    }
                    if (nv.TRINHDO.MaTDTH != null)
                    {
                        cbTrinhDoTinHoc.SelectedValue = nv.TRINHDO.MaTDTH; 
                    }
                    if (nv.TRINHDO.TRINH_DO_NGOAI_NGU != null)
                    {
                        if (nv.TRINHDO.TRINH_DO_NGOAI_NGU.maNN != null)
                        {
                            cbNgoaiNgu.SelectedValue = nv.TRINHDO.TRINH_DO_NGOAI_NGU.maNN;  
                        }
                    }
                    if (nv.TRINHDO.MaTDNN != null)
                    {
                        cbTrinhDoNN.SelectedValue = nv.TRINHDO.MaTDNN; 
                    }
                    if (nv.TRINHDO.maNoiDaoTao != null)
                    {
                        cbNoiDaoTao.SelectedValue = nv.TRINHDO.maNoiDaoTao; 
                    }
                    if (nv.TRINHDO.maHinhThuc != null)
                    {
                        cbHinhThucDaoTao.SelectedValue = nv.TRINHDO.maHinhThuc; 
                    }
                    if (nv.TRINHDO.maNganh != null)
                    {
                        cbNganh.SelectedValue = nv.TRINHDO.maNganh; 
                    }
                }
                #endregion
                ThongTinTrinhDo_Loaded = true;
            }
        }

        private void cbNgoaiNgu_SelectedValueChanged(object sender, EventArgs e)
        {
            //ComboBox cbTrinhDoNN
            if (cbNgoaiNgu.SelectedValue != null)
            {
                QLNS_TLDataContext dc = new QLNS_TLDataContext();
                var trinhdongoaingus = from dt in dc.TRINH_DO_NGOAI_NGUs
                                       where dt.maNN == (int)cbNgoaiNgu.SelectedValue
                                       select dt;
                try
                {
                    cbTrinhDoNN.DataSource = trinhdongoaingus;
                    cbTrinhDoNN.DisplayMember = "tenTrinhDoNN";
                    cbTrinhDoNN.ValueMember = "MaTDNN";
                }
                catch (Exception)
                {
                }
            }
            
        }

        private void tabPage1_Enter(object sender, EventArgs e)
        {
            if (!ThongTinTuyenDung_Loaded)
            {
                QLNS_TLDataContext dc = new QLNS_TLDataContext();
                #region Đổ dữ liệu vào control
                //ComboBox cbPhong
                var tdvh = from dt in dc.PhongBans
                           select dt;
                cbPhong.DataSource = tdvh;
                cbPhong.DisplayMember = "tenPhongBan";
                cbPhong.ValueMember = "maPhongBan";

                //ComboBox cbChucVu
                var nganhs = from dt in dc.ChucVus
                             select dt;
                cbChucVu.DataSource = nganhs;
                cbChucVu.DisplayMember = "tenChucVu";
                cbChucVu.ValueMember = "maChucVu";

                //ComboBox cbBacLuong
                var hinhthucdaotaos = from dt in dc.BacLuongs
                                      select dt;
                cbBacLuong.DataSource = hinhthucdaotaos;
                cbBacLuong.DisplayMember = "bac";
                cbBacLuong.ValueMember = "bac";

                #endregion

                #region Gán dự liệu cho control
                if (nv.maPhongBan != null)
                {
                    cbPhong.SelectedValue = nv.maPhongBan; 
                }
                if (nv.maChucVu != null)
                {
                    cbChucVu.SelectedValue = nv.maChucVu; 
                }
                try
                {
                    HopDong hd = LayHopDongHienTai(new QLNS_TLDataContext(), nv.maNV);
                    cbBacLuong.SelectedValue = hd.bac;
                }
                catch (Exception)
                {

                }
                checkBox1.Checked = (bool)nv.active;
                
                #endregion
                ThongTinTuyenDung_Loaded = true;
            }
        }

        public static HopDong LayHopDongHienTai(QLNS_TLDataContext dc, int MaNV)
        {
            try
            {
                return dc.HopDongs.SingleOrDefault(h => (h.maNV == MaNV && DateTime.Compare((DateTime)h.NOIDUNGHOPDONG.ngayBatDau, DateTime.Now) < 0 && DateTime.Compare((DateTime)h.NOIDUNGHOPDONG.ngayKetThuc, DateTime.Now) > 0));
            }
            catch (Exception)
            {
                var nv = dc.NhanViens.SingleOrDefault(n => n.maNV == MaNV);
                MessageBox.Show("Không tìm thấy hợp đồng còn thời hạn nào của nhân viên " + nv.hoTen);
                throw;
            }
        }

        private void GanGiaTri(QLNS_TLDataContext dc, NhanVien nv)
        {
            #region tabPage1
            if (txtHoTen.Text.Length > 1)
            {
                nv.hoTen = txtHoTen.Text;
            }
            else
            {
                throw new ArgumentNullException("Họ tên nhân viên không được rỗng.");
            }
            nv.tenGoiKhac = txtTenKhac.Text;
            nv.gioiTinh = rdNam.Checked;
            if (cbDanToc.SelectedValue != null)
            {
                nv.ThongTinCaNhan.maDanToc = (int)cbDanToc.SelectedValue; 
            }
            if (cbQuocTich.SelectedValue != null)
            {
                nv.ThongTinCaNhan.maQuocTich = (int)cbQuocTich.SelectedValue; 
            }
            nv.ngaySinh = dtpNgaySinh.Value;
            if (cbTonGiao.SelectedValue != null)
            {
                nv.ThongTinCaNhan.maTonGiao = (int)cbTonGiao.SelectedValue; 
            }
            nv.soDT = txtSdt.Text;
            nv.diaChiHienTai = txtDCHT.Text;
            nv.ThongTinCaNhan.diaChiThuongTru = txtDCTT.Text;
            nv.ThongTinCaNhan.queQuan = txtQueQuan.Text;
            nv.cmnd = txtCMND.Text;
            nv.ngayCap = dtpNgayCap.Value;
            nv.noiCap = txtNoiCap.Text;
            if (cbTinhTrangHonNhan.SelectedValue != null)
            {
                nv.ThongTinCaNhan.maTinhTrang = (int)cbTinhTrangHonNhan.SelectedValue; 
            }
            nv.ThongTinCaNhan.nangKhieu = txtNangKhieu.Text;
            #endregion

            #region tabPage2
            if (ThongTinTrinhDo_Loaded)
            {
                if (cbTrinhDoVanHoa.SelectedValue != null)
                {
                    nv.TRINHDO.MaHV = (int)cbTrinhDoVanHoa.SelectedValue; 
                }
                if (cbNganh.SelectedValue != null)
                {
                    nv.TRINHDO.maNganh = (int)cbNganh.SelectedValue; 
                }
                if (cbNoiDaoTao.SelectedValue != null)
                {
                    nv.TRINHDO.maNoiDaoTao = (int)cbNoiDaoTao.SelectedValue; 
                }
                if (cbHinhThucDaoTao.SelectedValue != null)
                {
                    nv.TRINHDO.maHinhThuc = (int)cbHinhThucDaoTao.SelectedValue; 
                }
                if (cbTrinhDoNN.SelectedValue != null)
                {
                    nv.TRINHDO.MaTDNN = (int)cbTrinhDoNN.SelectedValue; 
                }
            }
            #endregion

            #region tabPage3
            if (ThongTinTuyenDung_Loaded)
            {
                if (cbPhong.SelectedValue != null)
                {
                    nv.maPhongBan = (int)cbPhong.SelectedValue;
                }
                if (cbChucVu.SelectedValue != null)
                {
                    nv.maChucVu = (int)cbChucVu.SelectedValue;
                }
                if (cbBacLuong.SelectedValue != null)
                {
                    try
                    {
                        var hd = LayHopDongHienTai(dc, nv.maNV);
                        hd.bac = (int)cbBacLuong.SelectedValue;
                    }
                    catch (Exception)
                    {
                        //do nothing
                    }
                }
                nv.active = checkBox1.Checked;
            }
            #endregion
        }

        public static void ChuanHoaNhanVien(QLNS_TLDataContext dc, NhanVien nv)
        {
            if (nv.TRINHDO == null)
            {
                int maTDMax = 1;
                if (dc.TRINHDOs.Count() > 0)
                {
                    maTDMax = (from td in dc.TRINHDOs select td.maTrinhDo).Max();
                    maTDMax++;
                }
                TRINHDO tdo = new TRINHDO();
                tdo.maTrinhDo = maTDMax;
                dc.TRINHDOs.InsertOnSubmit(tdo);
                nv.TRINHDO = tdo;
            }
            if (nv.ThongTinCaNhan == null)
            {
                int maTTCNMax = 1;
                if (dc.ThongTinCaNhans.Count() > 0)
                {
                    maTTCNMax = (from td in dc.ThongTinCaNhans select td.maThongTinCn).Max();
                    maTTCNMax++;
                }
                ThongTinCaNhan tdo = new ThongTinCaNhan();
                tdo.maThongTinCn = maTTCNMax;
                dc.ThongTinCaNhans.InsertOnSubmit(tdo);
                nv.ThongTinCaNhan = tdo;
            }
        }

        private void btDongY_Click(object sender, EventArgs e)
        {
            QLNS_TLDataContext dc = new QLNS_TLDataContext();
            if (maNV > 0)
            {
                try
                {
                    nv = dc.NhanViens.Single(p => p.maNV == maNV);
                    ChuanHoaNhanVien(dc, nv);
                    GanGiaTri(dc, nv);
                    dc.SubmitChanges();
                    MessageBox.Show("Cập nhật thông tin nhân viên thành công!");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (ArgumentNullException argNullEx)
                {
                    MessageBox.Show(argNullEx.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Có lỗi xảy ra trong quá trình lưu thông tin nhân viên.\n\n" + ex.Message);
                }
            }
            else
            {
                try
                {
                    int maNVMax = 1;
                    if (dc.NhanViens.Count() > 0)
                    {
                        maNVMax = (from n in dc.NhanViens select n.maNV).Max();
                        maNVMax++;
                    }
                    nv.maNV = maNVMax;
                    ChuanHoaNhanVien(dc, nv);
                    GanGiaTri(dc, nv);
                    dc.NhanViens.InsertOnSubmit(nv);
                    dc.SubmitChanges();
                    MessageBox.Show("Thêm nhân viên thành công!");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (ArgumentNullException argNullEx)
                {
                    MessageBox.Show(argNullEx.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Có lỗi xảy ra trong quá trình thêm nhân viên.\n\n" + ex.Message);
                }
            }
        }

        private void cbBacLuong_SelectedValueChanged(object sender, EventArgs e)
        {
            QLNS_TLDataContext dc = new QLNS_TLDataContext();
            try
            {
                var bl = dc.BacLuongs.SingleOrDefault(b => b.bac == (int)cbBacLuong.SelectedValue);
                if (bl.heSo.HasValue)
                {
                    txtHeSo.Text = bl.heSo.Value.ToString(); 
                }
            }
            catch (Exception)
            {
                
            }
        }

    }
}
